<?php
namespace Portal\Controller;
use Common\Controller\HomebaseController;

class CardController extends HomebaseController {
    //添加新卡种
	public function addcard() {
		$this->display(":addcard");
		$a=implode(',',$_POST['canlesson']);
		$b=implode(',',$_POST['cansite']);
		//dump($a);
		//dump($_POST);
		//die();
		
		if($_POST['cardname']&&$_POST['canlesson']&&$_POST['cansite']){
			$Card=M('card');
			$arr['cardname']=$_POST['cardname'];
			$arr['cansite']=$b;
			$arr['canlesson']=$a;
			$msg=$Card->add($arr);
			if($msg){
				$this->succ('showcard');
			}
			else{
				$this->err('showcard');
			}
		}
		else{
			echo '请添加信息！';
		}
	}
	//卡种列表（修改、删除）
	public function clear(){
		$_POST=null;
		session("cardwhere['cansite']",null);
		$this->showcard();
	}
	public function showcard() {
		session(array('name'=>'session_id','expire'=>3600));
		$Card=M('card');
		if($_POST['cansite']){
			$where['cansite']=$_POST['cansite'];
			session("cardwhere['cansite']",$where['cansite']);
		}else if(!$_POST['cansite'] && session("cardwhere['cansite']")){
			$where['cansite']=session("cardwhere['cansite']");
		}
		$count=$Card->where($where)->count();
		$page=$this->page($count,10);
		$msg=$Card
		->where($where)
		->limit($page->firstRow , $page->listRows)
		->select();
		$this->assign('msg',$msg)
		->assign('where',$where)
		->assign("page", $page->show('Admin'));
    	$this->display(":showcard");
    }
	//删除卡种
	public function delcard() {
		$cardname=$_GET['cardname'];
		$Card=M('card');
		$msg=$Card->where("cardname='$cardname'")->delete();//where语句中的变量需要引号
		if($msg){
			$this->succ('showcard');
		}
	}
	//编辑卡种
	public function editcard() {
			$Card=M('card');
			$cardname=$_GET['cardname'];
			$msg=$Card->where("cardname='$cardname'")->select();
			$this->assign('msg',$msg);
			$this->display(":editcard");
			$a=implode(',',$_POST['canlesson']);
			$b=implode(',',$_POST['cansite']);
			$arr['cardname']=$_POST['cardname'];
			$arr['cansite']=$b;
			$arr['canlesson']=$a;
			if($_POST['cardname']&&$_POST['cansite']&&$_POST['canlesson']){

				$msg=$Card->where("cardname='$cardname'")->save($arr);
				if($msg){
					$this->succ('showcard');
				}
				else{
					$this->err('showcard');
				}
			}
	}
	//操作成功
	public function succ($m){
		$C='Card';
		$this->assign('m',$m)->assign('C',$C);
		$this->display(':success');
	}
	//操作失败
	public function err($m){
		$C='Card';
		$this->assign('m',$m)->assign('C',$C);		
		$this->display(':err');
	}
}
